package koala

import (
	"encoding/json"
	"errors"
	"gitee.com/wuzheng0709/backend-gopkg/infrastructure/pkg/gin/log"
	"gitee.com/wuzheng0709/backend-gopkg/infrastructure/pkg/httpclient"
	"github.com/gin-gonic/gin"
	"net/http"
)

// 判断用户行为是否被允许
func BehaviorIsAccess(koalaUrl string) (isAccess bool, err error) {
	hc := make(gin.HandlersChain, 0)
	resBody, resCode, resErr := httpclient.PublicRequire(koalaUrl,
		"GET",
		[]byte{}, &http.Header{}, hc,
		nil)
	if resCode != 200 {
		return false, resErr
	}

	var resMap map[string]interface{}
	if err := json.Unmarshal(resBody, &resMap); err != nil {
		log.Error("反序列化失败，err:", err.Error())
		return false, err
	}
	if resMap["Str_reason"].(string) == "Allow" {
		return true, nil
	}
	return false, errors.New("禁止触发当前行为")
}
